package logic

import (
	"context"
	"doctor-medicine-system/model/mysql"
	"errors"

	"doctor-medicine-system/dockerrpc/dockerrpc"
	"doctor-medicine-system/dockerrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 医生登录
func (l *LoginLogic) Login(in *dockerrpc.LoginRequest) (*dockerrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var docker mysql.Docker

	err := docker.Login(mysql.DB, in.NameCode)
	if err != nil {
		return nil, err
	}

	if in.NameCode != docker.NameCode {
		return nil, errors.New("该用户未注册 请先注册")
	}

	if in.Password != docker.Password {
		return nil, errors.New("密码错误 登录失败")
	}

	return &dockerrpc.LoginResponse{
		DockerId: int64(docker.ID),
	}, nil
}
